using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTron.Model;
using iTron.Model.Player;
using iTron.Model.GridItems;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace iTron.View
{
    class GridGUI
    {
        public int gameStatus { get; set; }
        BikeView bikeHuman, bikeAI;
        TrailView trailHuman, trailAI;
        GameTime gameTime;
        Grid grid;
        List<Player> players = new List<Player>();

        public GridGUI(ContentManager cm, Grid grid)
        {
            this.grid = grid;
            bikeHuman = new BikeView(cm, "bike_red");
            bikeAI = new BikeView(cm, "bike_yellow");

            trailHuman = new TrailView(cm, "trail_red");
            trailAI = new TrailView(cm, "trail_yellow");
        }

        public void addPlayer(Player player)
        {
            players.Add(player);
            grid.addPlayer(player);

        }

        public void draw(SpriteBatch batch, GridItem[,] grid/*, Vector2 vecpos*/)
        {
            Vector2 position;
            for (int i = 0; i < Grid.GRID_HEIGHT; i++)
            {
                for (int j = 0; j < Grid.GRID_WIDTH; j++)
                {
                    if (grid[i, j].isTrail)
                    {
                        position = new Vector2((i * TrailView.SPRITE_HEIGHT),
                            ((15 - j) * TrailView.SPRITE_WIDTH));
                        int dirFrom = grid[i, j].directionFrom;
                        int dirTo = grid[i, j].directionTo;

                        if (grid[i, j].playerID == 1)
                        {
                            batch.Draw(trailHuman.texture, position, trailHuman.getRectangle(grid[i,j], false), Color.WhiteSmoke,
                            -1.570796f, new Vector2(+37f, -100f), 1.0f, SpriteEffects.None, 0.0f);
                        }
                        else
                        {
                            batch.Draw(trailAI.texture, position, trailAI.getRectangle(grid[i, j], false), Color.WhiteSmoke,
                            -1.570796f, new Vector2(+37f, -100f), 1.0f, SpriteEffects.None, 0.0f);
                        }
                    }
                }
            }
            for (int i = 0; i < Grid.GRID_HEIGHT; i++)
            {
                for (int j = 0; j < Grid.GRID_WIDTH; j++)
                {
                    if (grid[i, j].isBike)
                    {
                        position = new Vector2((i * BikeView.SPRITE_HEIGHT),
                            ((15 - j) * BikeView.SPRITE_WIDTH));
                        int dir = grid[i, j].directionTo;

                        if (grid[i, j].playerID == 1)
                        {
                            // Todo:
                            batch.Draw(trailHuman.texture, trailHuman.getPosition((Bike)grid[i, j], position), trailAI.getRectangle((Bike)grid[i, j]), Color.WhiteSmoke,
                            -1.570796f, new Vector2(+37f, -100f), 1.0f, SpriteEffects.None, 0.0f); // TODO END
                            batch.Draw(bikeHuman.texture, bikeHuman.getPosition((Bike)grid[i, j], position), bikeHuman.getRectangle(dir - 1), Color.WhiteSmoke,
                            -1.570796f, new Vector2(+37f, -100f), 1.0f, SpriteEffects.None, 0.0f);
                        }
                        else
                        {
                            // Todo:
                            batch.Draw(trailAI.texture, trailAI.getPosition((Bike)grid[i, j], position), trailHuman.getRectangle((Bike)grid[i, j]), Color.WhiteSmoke,
                            -1.570796f, new Vector2(+37f, -100f), 1.0f, SpriteEffects.None, 0.0f); // TODO END
                            batch.Draw(bikeAI.texture, bikeAI.getPosition((Bike)grid[i, j], position), bikeAI.getRectangle(dir - 1), Color.WhiteSmoke,
                            -1.570796f, new Vector2(+37f, -100f), 1.0f, SpriteEffects.None, 0.0f);
                        }

                    }
                }
            }
        }

        public void update(GameTime time)
        {
            this.gameTime = time;
            // TODO: If explossion?
        }
    }
}
